package com.song.test.controller;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class GaodeApi {
	 
    public static void main(String[] args) throws Exception {
    	//http://restapi.amap.com/v3/ip?ip=114.247.50.2&output=xml&key=<用户的key>
    	String url = "/v3/ip";  
        String host = "restapi.amap.com";  
        String param = "ip=114.247.50.2&output=json&key=e8d39840feea3f0643cab8eba29db9b3";  
        HttpClient httpClient = new HttpClient();  
        httpClient.getHostConfiguration().setHost(host, 80, "http");          
          
        HttpMethod method = getMethod(url, param);  
        //HttpMethod method = postMethod(url);  
          
        httpClient.executeMethod(method);  
          
        String response = method.getResponseBodyAsString();  
        //String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));                  
        System.out.println(response);
	}
    
    private static HttpMethod getMethod(String url,String param) throws IOException{  
        GetMethod get = new GetMethod(url+"?"+param);  
        get.releaseConnection();  
        return get;  
    } 
    
    
    private static HttpMethod postMethod(String url) throws IOException{   
        PostMethod post = new PostMethod(url);  
        post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");    
        NameValuePair[] param = { new NameValuePair("startCity","杭州"),  
                new NameValuePair("lastCity","沈阳"),  
                new NameValuePair("userID",""),  
                new NameValuePair("theDate","") } ;  
        post.setRequestBody(param);  
        post.releaseConnection();  
        return post;  
    }  
	
}
